home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / ExternalShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  22.0 KB  |  710 lines

  1. /* External shell widget.
  2.    Copyright (C) 1993, 1994 Sun Microsystems, Inc.
  3.  
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8.  
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free
  16. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17.  
  18. /* Synched up with: Not in FSF. */
  19.  
  20. /* Written by Ben Wing, September 1993. */
  21.  
  22. /* This is a special Shell that is designed to use an externally-
  23.    provided window created by someone else (possibly another process).
  24.    That other window should have an associated widget of class
  25.    ExternalClient.  The two widgets communicate with each other using
  26.    ClientMessage events and properties on the external window.
  27.  
  28.    Ideally this feature should be independent of Emacs.  Unfortunately
  29.    there are lots and lots of specifics that need to be dealt with
  30.    for this to work properly, and some of them can't conveniently
  31.    be handled within the widget's methods.  Some day the code may
  32.    be rewritten so that the embedded-widget feature can be used by
  33.    any application, with appropriate entry points that are called
  34.    at specific points within the application.
  35.  
  36.    This feature is similar to the OLE (Object Linking & Embedding)
  37.    feature provided by MS Windows.
  38.  */
  39.  
  40. #ifdef emacs
  41.  
  42. #include <config.h>
  43.  
  44. #ifndef EXTERNAL_WIDGET
  45. ERROR!  This ought not be getting compiled if EXTERNAL_WIDGET is undefined
  46. #endif
  47.  
  48. #endif /* emacs */
  49.  
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <X11/StringDefs.h>
  53. #include "xintrinsicp.h"
  54. #include <X11/Shell.h>
  55. #include <X11/ShellP.h>
  56. #include <X11/Vendor.h>
  57. #include <X11/VendorP.h>
  58. #include "ExternalShellP.h"
  59. #include "extw-Xt.h"
  60.  
  61. #ifdef emacs
  62. extern void emacs_Xt_handle_focus_event (XEvent *event);
  63. #endif
  64.  
  65. /* Communication between this shell and the client widget:
  66.  
  67.    Communication is through ClientMessage events with message_type
  68.    EXTW_NOTIFY and format 32.  Both the shell and the client widget
  69.    communicate with each other by sending the message to the same
  70.    window (the "external window" below), and the data.l[0] value is
  71.    used to determine who sent the message.
  72.  
  73.    The data is formatted as follows:
  74.  
  75.    data.l[0] = who sent this message: external_shell_send (0) or
  76.                external_client_send (1)
  77.    data.l[1] = message type (see enum en_extw_notify below)
  78.    data.l[2-4] = data associated with this message
  79.  
  80.    EventHandler() handles messages from the other side.
  81.  
  82.    extw_send_notify_3() sends a message to the other side.
  83.  
  84.    extw_send_geometry_value() is used when an XtWidgetGeometry structure
  85.       needs to be sent.  This is too much data to fit into a
  86.       ClientMessage, so the data is stored in a property and then
  87.       extw_send_notify_3() is called.
  88.  
  89.    extw_get_geometry_value() receives an XtWidgetGeometry structure from a
  90.       property.
  91.  
  92.    extw_wait_for_response() is used when a response to a sent message
  93.       is expected.  It looks for a matching event within a
  94.       particular timeout.
  95.  
  96.    The particular message types are as follows:
  97.  
  98. 1) extw_notify_init (event_window, event_mask)
  99.  
  100.    This is sent from the shell to the client after the shell realizes
  101.    its EmacsFrame widget on the client's "external window".  This
  102.    tells the client that it should start passing along events of the
  103.    types specified in event_mask.  event_window specifies the window
  104.    of the EmacsFrame widget, which is a child of the client's
  105.    external window.
  106.  
  107.    extw_notify_init (client_type)
  108.  
  109.    When the client receives an extw_notify_init message from the
  110.    shell, it sends back a message of the same sort specifying the type
  111.    of the toolkit used by the client (Motif, generic Xt, or Xlib).
  112.  
  113. 2) extw_notify_end ()
  114.  
  115.    This is sent from the shell to the client when the shell's
  116.    EmacsFrame widget is destroyed, and tells the client to stop
  117.    passing events along.
  118.  
  119. 3) extw_notify_qg (result)
  120.  
  121.    This is sent from the client to the shell when a QueryGeometry
  122.    request is received on the client.  The XtWidgetGeometry structure
  123.    specified in the QueryGeometry request is passed on in the
  124.    EXTW_QUERY_GEOMETRY property (of type EXTW_WIDGET_GEOMETRY) on the
  125.    external window.  result is unused.
  126.  
  127.    In response, the shell passes the QueryGeometry request down the
  128.    widget tree, and when a response is received, sends a message of
  129.    type extw_notify_qg back to the client, with result specifying the
  130.    GeometryResult value.  If this value is XtGeometryAlmost, the
  131.    returned XtWidgetGeometry structure is stored into the same property
  132.    as above. [BPW is there a possible race condition here?]
  133.  
  134. 4) extw_notify_gm (result)
  135.  
  136.    A very similar procedure to that for extw_notify_qg is followed
  137.    when the shell's RootGeometryManager method is called, indicating
  138.    that a child widget wishes to change the shell's geometry.  The
  139.    XtWidgetGeometry structure is stored in the EXTW_GEOMETRY_MANAGER
  140.    property.
  141.  
  142. 5) extw_notify_focus_in (), extw_notify_focus_out ()
  143.  
  144.    These are sent from the client to the shell when the client gains
  145.    or loses the keyboard focus.  It is done this way because Xt
  146.    maintains its own concept of keyboard focus and only the client
  147.    knows this information.
  148. */
  149.  
  150. #define NOTIFY(w, type, l0, l1, l2) \
  151.   extw_send_notify_3(XtDisplay((Widget)(w)),\
  152.    (w)->externalShell.external_window, type, l0, l1, l2)
  153.  
  154. static void ExternalShellInitialize (Widget req, Widget new, ArgList args,
  155.                      Cardinal *num_args);
  156. static void ExternalShellRealize (Widget wid, Mask *vmask, XSetWindowAttributes
  157.                   *attr);
  158. static void ExternalShellDestroy (Widget w);
  159. static void ChangeManaged (Widget wid);
  160. static XtGeometryResult ExternalShellRootGeometryManager (Widget gw,
  161.   XtWidgetGeometry *request, XtWidgetGeometry *reply);
  162. static void EventHandler (Widget wid, XtPointer closure, XEvent *event,
  163.               Boolean *continue_to_dispatch);
  164.  
  165. #ifndef DEFAULT_WM_TIMEOUT
  166. # define DEFAULT_WM_TIMEOUT 5000
  167. #endif
  168.  
  169. void ExternalShellUnrealize (Widget w);
  170.  
  171. static XtResource resources[] = {
  172. #define offset(field) XtOffset(ExternalShellWidget, externalShell.field)
  173.   { XtNwindow, XtCWindow, XtRWindow, sizeof (Window),
  174.       offset (external_window), XtRImmediate, (XtPointer)0},
  175.   { XtNclientTimeout, XtCClientTimeout, XtRInt, sizeof(int),
  176.       offset(client_timeout), XtRImmediate,(XtPointer)DEFAULT_WM_TIMEOUT},
  177.   { XtNdeadClient, XtCDeadClient, XtRBoolean, sizeof(Boolean),
  178.       offset(dead_client), XtRImmediate, (XtPointer)False},
  179. };
  180.  
  181. static CompositeClassExtensionRec compositeClassExtRec = {
  182.     NULL,
  183.     NULLQUARK,
  184.     XtCompositeExtensionVersion,
  185.     sizeof(CompositeClassExtensionRec),
  186.     TRUE,
  187. };
  188.  
  189. static ShellClassExtensionRec shellClassExtRec = {
  190.     NULL,
  191.     NULLQUARK,
  192.     XtShellExtensionVersion,
  193.     sizeof(ShellClassExtensionRec),
  194.     ExternalShellRootGeometryManager
  195. };
  196.  
  197. ExternalShellClassRec externalShellClassRec = {
  198.     { /*
  199.        *    core_class fields
  200.        */
  201.     /* superclass      */    (WidgetClass) &shellClassRec,
  202.     /* class_name      */    "ExternalShell",
  203.     /* size          */    sizeof(ExternalShellRec),
  204.     /* Class Initializer  */    NULL,
  205.     /* class_part_initialize*/    NULL, /* XtInheritClassPartInitialize, */
  206.     /* Class init'ed ?      */    FALSE,
  207.     /* initialize      */    ExternalShellInitialize,
  208.     /* initialize_notify  */    NULL,
  209.     /* realize          */    ExternalShellRealize,
  210.     /* actions          */    NULL,
  211.     /* num_actions      */    0,
  212.     /* resources      */    resources,
  213.     /* resource_count      */    XtNumber (resources),
  214.     /* xrm_class      */    NULLQUARK,
  215.     /* compress_motion      */    FALSE,
  216.     /* compress_exposure  */    TRUE,
  217.     /* compress_enterleave*/    FALSE,
  218.     /* visible_interest      */    TRUE,
  219.     /* destroy          */    ExternalShellDestroy, /* XtInheritDestroy, */
  220.     /* resize          */    XtInheritResize,
  221.     /* expose          */    NULL,
  222.     /* set_values      */    NULL, /* XtInheritSetValues, */
  223.     /* set_values_hook      */    NULL,            
  224.     /* set_values_almost  */    XtInheritSetValuesAlmost,  
  225.     /* get_values_hook      */    NULL,            
  226.     /* accept_focus      */    NULL,
  227.     /* intrinsics version */    XtVersion,
  228.     /* callback offsets      */    NULL,
  229.     /* tm_table          */    NULL,
  230.     /* query_geometry      */    NULL,
  231.     /* display_accelerator*/    NULL,
  232.     /* extension      */    NULL
  233.   },{ /* Composite */
  234.     /* geometry_manager      */    XtInheritGeometryManager,
  235.     /* change_managed      */    ChangeManaged,  /* XtInheritChangeManaged */
  236.     /* insert_child      */    XtInheritInsertChild,
  237.     /* delete_child      */    XtInheritDeleteChild,
  238.     /* extension      */    (XtPointer)&compositeClassExtRec
  239.   },{ /* Shell */
  240.     /* extension      */    (XtPointer)&shellClassExtRec
  241.   },{ /* ExternalShell */
  242.     0
  243.   }
  244. };
  245.  
  246. WidgetClass externalShellWidgetClass = (WidgetClass) &externalShellClassRec;
  247.  
  248. static void ExternalShellInitialize (Widget req, Widget new, ArgList args,
  249.                   Cardinal *num_args)
  250. {
  251.   XtAddEventHandler(new, NULL,
  252.             TRUE, EventHandler, (XtPointer) NULL);
  253.   extw_initialize_atoms(XtDisplay(req));
  254.   extw_which_side = extw_shell_send;
  255. }
  256.  
  257. static Widget find_managed_child(CompositeWidget w)
  258. {
  259.   int i;
  260.   Widget *childP = w->composite.children;
  261.  
  262.   for (i = w->composite.num_children; i; i--, childP++)
  263.     if (XtIsWidget(*childP) && XtIsManaged(*childP))
  264.       return *childP;
  265.   return NULL;
  266. }
  267.  
  268. #ifndef XtCXtToolkitError
  269. # define XtCXtToolkitError "XtToolkitError"
  270. #endif
  271.  
  272. static void EventHandler(wid, closure, event, continue_to_dispatch)
  273.      Widget wid;
  274.      XtPointer closure;    /* unused */
  275.      XEvent *event;
  276.      Boolean *continue_to_dispatch; /* unused */
  277. {
  278.   ExternalShellWidget w = (ExternalShellWidget) wid;
  279.  
  280.   if(w->core.window != event->xany.window) {
  281.     XtAppErrorMsg(XtWidgetToApplicationContext(wid),
  282.           "invalidWindow","eventHandler",XtCXtToolkitError,
  283.           "Event with wrong window",
  284.           (String *)NULL, (Cardinal *)NULL);
  285.     return;
  286.   }
  287.  
  288.   if (event->type == ClientMessage &&
  289.       event->xclient.data.l[0] == extw_client_send &&
  290.       event->xclient.message_type == a_EXTW_NOTIFY)
  291.     switch (event->xclient.data.l[1]) {
  292.  
  293.     case extw_notify_gm:
  294.       /* client is alive again. */
  295.       w->externalShell.dead_client = False;
  296.       break;
  297.  
  298.     case extw_notify_qg: {
  299.       XtWidgetGeometry xwg, xwg_return;
  300.       XtGeometryResult result;
  301.       Widget child = find_managed_child((CompositeWidget) w);
  302.  
  303.       if (child) {
  304.     extw_get_geometry_value(XtDisplay(wid), XtWindow(wid),
  305.                 a_EXTW_QUERY_GEOMETRY, &xwg);
  306.     result = XtQueryGeometry(child, &xwg, &xwg_return);
  307.       } else
  308.     result = XtGeometryYes;
  309.  
  310.       extw_send_geometry_value(XtDisplay(wid), XtWindow(wid),
  311.                    a_EXTW_QUERY_GEOMETRY, extw_notify_qg,
  312.                    result == XtGeometryAlmost ? &xwg_return :
  313.                    NULL, result);
  314.       break;
  315.     }
  316.  
  317.     case extw_notify_focus_in: {
  318.       XFocusChangeEvent event;
  319.       
  320.       event.type = FocusIn;
  321.       event.serial = LastKnownRequestProcessed (XtDisplay (wid));
  322.       event.send_event = True;
  323.       event.display = XtDisplay (wid);
  324.       event.window = XtWindow (wid);
  325.       event.mode = NotifyNormal;
  326.       event.detail = NotifyAncestor;
  327. #ifdef emacs
  328.       emacs_Xt_handle_focus_event ((XEvent *) &event);
  329. #else
  330.       XtDispatchEvent ((XEvent *) &event);
  331. #endif
  332.       break;
  333.     }
  334.       
  335.     case extw_notify_focus_out: {
  336.       XFocusChangeEvent event;
  337.       
  338.       event.type = FocusOut;
  339.       event.serial = LastKnownRequestProcessed (XtDisplay (wid));
  340.       event.send_event = True;
  341.       event.display = XtDisplay (wid);
  342.       event.window = XtWindow (wid);
  343.       event.mode = NotifyNormal;
  344.       event.detail = NotifyAncestor;
  345. #ifdef emacs
  346.       emacs_Xt_handle_focus_event ((XEvent *) &event);
  347. #else
  348.       XtDispatchEvent ((XEvent *) &event);
  349. #endif
  350.       break;
  351.     }
  352.  
  353.     case extw_notify_end:
  354.       /* frame should be destroyed. */
  355.       break;
  356.     }
  357. }
  358.  
  359. /* Lifted almost entirely from GetGeometry() in Shell.c
  360.  */
  361. static void GetGeometry(W, child)
  362.     Widget W, child;
  363. {
  364.     ExternalShellWidget w = (ExternalShellWidget)W;
  365.     int x, y, win_gravity = -1, flag;
  366.     XSizeHints hints;
  367.     Window win = w->externalShell.external_window;
  368.     
  369.     {
  370.       Window dummy_root;
  371.       unsigned int dummy_bd_width, dummy_depth, width, height;
  372.       
  373.       /* determine the existing size of the window. */
  374.       XGetGeometry(XtDisplay(W), win, &dummy_root, &x, &y, &width,
  375.            &height, &dummy_bd_width, &dummy_depth);
  376.       w->core.width = width;
  377.       w->core.height = height;
  378.     }
  379.  
  380.     if(w->shell.geometry != NULL) {
  381.     char def_geom[64];
  382.     int width, height;
  383.  
  384.     x = w->core.x;
  385.     y = w->core.y;
  386.     width = w->core.width;
  387.     height = w->core.height;
  388.     hints.flags = 0;
  389.  
  390.     sprintf( def_geom, "%dx%d+%d+%d", width, height, x, y );
  391.     flag = XWMGeometry( XtDisplay(W),
  392.                 XScreenNumberOfScreen(XtScreen(W)),
  393.                 w->shell.geometry, def_geom,
  394.                 (unsigned int)w->core.border_width,
  395.                 &hints, &x, &y, &width, &height,
  396.                 &win_gravity
  397.                );
  398.     if (flag) {
  399.         if (flag & XValue) w->core.x = (Position)x;
  400.         if (flag & YValue) w->core.y = (Position)y;
  401.         if (flag & WidthValue) w->core.width = (Dimension)width;
  402.         if (flag & HeightValue) w->core.height = (Dimension)height;
  403.     }
  404.     else {
  405.         String params[2];
  406.         Cardinal num_params = 2;
  407.         params[0] = XtName(W);
  408.         params[1] = w->shell.geometry;
  409.         XtAppWarningMsg(XtWidgetToApplicationContext(W),
  410.        "badGeometry", "shellRealize", XtCXtToolkitError,
  411.        "Shell widget \"%s\" has an invalid geometry specification: \"%s\"",
  412.                 params, &num_params);
  413.     }
  414.     }
  415.     else
  416.     flag = 0;
  417.  
  418.     w->shell.client_specified |= _XtShellGeometryParsed;
  419. }
  420.  
  421. /* Lifted almost entirely from Realize() in Shell.c
  422.  */
  423. static void ExternalShellRealize (Widget wid, Mask *vmask,
  424.                   XSetWindowAttributes *attr)
  425. {
  426.     ExternalShellWidget w = (ExternalShellWidget) wid;
  427.         Mask mask = *vmask;
  428.     Window win = w->externalShell.external_window;
  429.  
  430.     if (!win) {
  431.       Cardinal count = 1;
  432.       XtErrorMsg("invalidWindow","shellRealize", XtCXtToolkitError,
  433.              "No external window specified for ExternalShell widget %s",
  434.              &wid->core.name, &count);
  435.     }
  436.  
  437.     if (! (w->shell.client_specified & _XtShellGeometryParsed)) {
  438.         /* we'll get here only if there was no child the first
  439.            time we were realized.  If the shell was Unrealized
  440.            and then re-Realized, we probably don't want to
  441.            re-evaluate the defaults anyway.
  442.          */
  443.         GetGeometry(wid, (Widget)NULL);
  444.     }
  445.     else if (w->core.background_pixmap == XtUnspecifiedPixmap) {
  446.         /* I attempt to inherit my child's background to avoid screen flash
  447.          * if there is latency between when I get resized and when my child
  448.          * is resized.  Background=None is not satisfactory, as I want the
  449.          * user to get immediate feedback on the new dimensions (most
  450.          * particularly in the case of a non-reparenting wm).  It is
  451.          * especially important to have the server clear any old cruft
  452.          * from the display when I am resized larger.
  453.          */
  454.         Widget *childP = w->composite.children;
  455.         int i;
  456.         for (i = w->composite.num_children; i; i--, childP++) {
  457.         if (XtIsWidget(*childP) && XtIsManaged(*childP)) {
  458.             if ((*childP)->core.background_pixmap
  459.                 != XtUnspecifiedPixmap) {
  460.             mask &= ~(CWBackPixel);
  461.             mask |= CWBackPixmap;
  462.             attr->background_pixmap =
  463.                 w->core.background_pixmap =
  464.                 (*childP)->core.background_pixmap;
  465.             } else {
  466.             attr->background_pixel = 
  467.                 w->core.background_pixel = 
  468.                 (*childP)->core.background_pixel;
  469.             }
  470.             break;
  471.         }
  472.         }
  473.     }
  474.  
  475.     if(w->shell.save_under) {
  476.         mask |= CWSaveUnder;
  477.         attr->save_under = TRUE;
  478.     }
  479.     if(w->shell.override_redirect) {
  480.         mask |= CWOverrideRedirect;
  481.         attr->override_redirect = TRUE;
  482.     }
  483.     if (wid->core.width == 0 || wid->core.height == 0) {
  484.         Cardinal count = 1;
  485.         XtErrorMsg("invalidDimension", "shellRealize", XtCXtToolkitError,
  486.                "Shell widget %s has zero width and/or height",
  487.                &wid->core.name, &count);
  488.     }
  489.     wid->core.window = win;
  490.     XChangeWindowAttributes(XtDisplay(wid), wid->core.window,
  491.                 mask, attr);
  492.  
  493. }
  494.  
  495. static void ExternalShellDestroy(wid)
  496.     Widget wid;
  497. {
  498.   ExternalShellWidget w = (ExternalShellWidget)wid;
  499.  
  500.   if (XtIsRealized(wid))
  501.     ExternalShellUnrealize(wid);
  502.  
  503.   NOTIFY(w, extw_notify_end, 0, 0, 0);
  504. }
  505.  
  506. /* Invoke matching routine from superclass, but first override its
  507.    geometry opinions with our own routine */
  508.  
  509. static void ChangeManaged(wid)
  510.     Widget wid;
  511. {
  512.   if (!XtIsRealized (wid))
  513.     GetGeometry(wid, (Widget)NULL);
  514.   (*((ShellClassRec*)externalShellClassRec.core_class.superclass)->
  515.    composite_class.change_managed)(wid);
  516. }
  517.  
  518. /* Based on RootGeometryManager() in Shell.c */
  519.  
  520. static XtGeometryResult ExternalShellRootGeometryManager(gw, request, reply)
  521.     Widget gw;
  522.     XtWidgetGeometry *request, *reply;
  523. {
  524.     ExternalShellWidget w = (ExternalShellWidget)gw;
  525.     unsigned int mask = request->request_mode;
  526.     XEvent event;
  527.     int oldx, oldy, oldwidth, oldheight, oldborder_width;
  528.     unsigned long request_num;
  529.     XtWidgetGeometry req = *request; /* don't modify caller's structure */
  530.  
  531.     oldx = w->core.x;
  532.     oldy = w->core.y;
  533.     oldwidth = w->core.width;
  534.     oldheight = w->core.height;
  535.     oldborder_width = w->core.border_width;
  536.  
  537. #define PutBackGeometry() \
  538.     { w->core.x = oldx; \
  539.       w->core.y = oldy; \
  540.       w->core.width = oldwidth; \
  541.       w->core.height = oldheight; \
  542.       w->core.border_width = oldborder_width; }
  543.  
  544.     if (mask & CWX) {
  545.       if (w->core.x == request->x) mask &= ~CWX;
  546.       else
  547.     w->core.x = request->x;
  548.     }
  549.     if (mask & CWY) {
  550.       if (w->core.y == request->y) mask &= ~CWY;
  551.       else w->core.y = request->y;
  552.     }
  553.     if (mask & CWBorderWidth) {
  554.       if (w->core.border_width == request->border_width)
  555.           mask &= ~CWBorderWidth;
  556.       else w->core.border_width = request->border_width;
  557.     }
  558.     if (mask & CWWidth) {
  559.       if (w->core.width == request->width) mask &= ~CWWidth;
  560.       else w->core.width = request->width;
  561.     }
  562.     if (mask & CWHeight) {
  563.       if (w->core.height == request->height) mask &= ~CWHeight;
  564.       else w->core.height = request->height;
  565.     }
  566.  
  567.     if (!XtIsRealized((Widget)w)) return XtGeometryYes;
  568.  
  569.     req.sibling = None;
  570.     req.request_mode = mask & ~CWSibling;
  571.     request_num = NextRequest(XtDisplay(w));
  572.     extw_send_geometry_value(XtDisplay(w), XtWindow(w),
  573.                  a_EXTW_GEOMETRY_MANAGER,
  574.                  extw_notify_gm, &req, 0);
  575.  
  576.     if (w->externalShell.dead_client == TRUE) {
  577.       /* The client is sick.  Refuse the request.
  578.        * If the client recovers and decides to honor the
  579.        * request, it will be handled by Shell's EventHandler().
  580.        */
  581.       PutBackGeometry();
  582.       return XtGeometryNo;
  583.     }
  584.  
  585.     if (extw_wait_for_response(gw, &event, request_num, extw_notify_gm,
  586.                    w->externalShell.client_timeout)) {
  587.       XtGeometryResult result = (XtGeometryResult) event.xclient.data.l[2];
  588.  
  589.       if (result != XtGeometryYes)
  590.     PutBackGeometry();
  591.       if (result == XtGeometryAlmost) {
  592.     extw_get_geometry_value(XtDisplay(w), XtWindow(w),
  593.                 a_EXTW_GEOMETRY_MANAGER, reply);
  594.       }
  595.       return result;
  596.     } else {
  597.       w->externalShell.dead_client = TRUE; /* timed out; must be broken */
  598.       PutBackGeometry();
  599.       return XtGeometryNo;
  600.     }
  601. #undef PutBackGeometry
  602. }
  603.  
  604. static void
  605. hack_event_masks_1 (Display *display, Window w, int this_window_propagate)
  606. {
  607.   Window root, parent, *children;
  608.   unsigned int nchildren;
  609.   int i;
  610.  
  611.   if (!XQueryTree (display, w, &root, &parent, &children, &nchildren))
  612.     return;
  613.   for (i=0; i<nchildren; i++)
  614.     hack_event_masks_1 (display, children[i], 1);
  615.   if (children)
  616.     XFree (children);
  617.   {
  618.     XWindowAttributes xwa;
  619.     XSetWindowAttributes xswa;
  620.     if (XGetWindowAttributes (display, w, &xwa)) {
  621.       xswa.event_mask = xwa.your_event_mask & ~KeyPressMask;
  622.       if (this_window_propagate)
  623.     xswa.do_not_propagate_mask = xwa.do_not_propagate_mask & ~KeyPressMask;
  624.       XChangeWindowAttributes (display, w, CWEventMask, &xswa);
  625.     }
  626.   }
  627. }
  628.  
  629. /* fix all event masks on all subwindows of the specified window so that
  630.    all key presses in any subwindow filter up to the specified window.
  631.  
  632.    We have to do this cruftiness with external widgets so that we don't
  633.    step on Motif's concept of keyboard focus.  (Due to the nature of
  634.    Xt and Motif, X's idea of who gets the keyboard events may not jive
  635.    with Xt's idea of same, and Xt redirects the events to the proper
  636.    window.  This occurs on the client side and we have no knowledge
  637.    of it, so we have to rely on a SendEvent from the client side to
  638.    receive our keyboard events.)
  639. */
  640.  
  641. static void
  642. hack_event_masks (Display *display, Window w)
  643. {
  644.   hack_event_masks_1 (display, w, 0);
  645. }
  646.  
  647. /* external entry points */
  648.  
  649. Bool
  650. ExternalShellReady (Widget w, Window win, long event_mask)
  651. {
  652.   ExternalShellWidget ew = (ExternalShellWidget) w;
  653.   XEvent event;
  654.   unsigned long request_num;
  655.  
  656.   request_num = NextRequest(XtDisplay(w));
  657.   NOTIFY(ew, extw_notify_init, (long) win, event_mask, 0);
  658.   if (extw_wait_for_response(w, &event, request_num, extw_notify_init,
  659.                  ew->externalShell.client_timeout))
  660.     {
  661.       /* Xt/Xm extw's have more elaborate focus needs than mere
  662.      Xlib ones.
  663.  
  664.      Rather independently, they *don't* need the
  665.      ConfigureNotify event, having fixed up the window size in
  666.      ChangeManaged, above, but Xlib extw's do need this.
  667.       */
  668.       ew->externalShell.client_type = event.xclient.data.l[2];
  669.       if (ew->externalShell.client_type != EXTW_TYPE_XLIB)
  670.     {
  671.       hack_event_masks (XtDisplay (w), XtWindow (w));
  672.     }
  673.       else
  674.     {
  675.       XConfigureEvent ev;
  676.       XWindowAttributes xwa;
  677.       ev.type = ConfigureNotify;
  678.       ev.display = XtDisplay (w);
  679.       ev.event = ev.window = XtWindow (w);
  680.       XGetWindowAttributes (ev.display, ev.window, &xwa);
  681.       ev.x = xwa.x; ev.y = xwa.y;
  682.       ev.width = xwa.width; ev.height = xwa.height;
  683.       ev.border_width = xwa.border_width;
  684.       ev.above = None;
  685.       ev.override_redirect = xwa.override_redirect;
  686.       XtDispatchEvent ((XEvent *) &ev);
  687.     }
  688.       return TRUE;
  689.     }
  690.   else
  691.     return FALSE;
  692. }
  693.  
  694. void
  695. ExternalShellSetFocus (Widget wid)
  696. {
  697.   ExternalShellWidget w = (ExternalShellWidget) wid;
  698.  
  699.   NOTIFY(w, extw_notify_set_focus, 0, 0, 0);
  700. }
  701.  
  702. extern void _XtUnregisterWindow (Window, Widget);
  703.  
  704. void
  705. ExternalShellUnrealize (Widget w)
  706. {
  707.   _XtUnregisterWindow (w->core.window, w);
  708.   w->core.window = 0;
  709. }
  710.